package com.cash.app.ui.auth

import android.content.Context
import android.widget.Toast
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.cash.app.R
import com.cash.app.base.BaseViewModel
import com.cash.app.constant.AppConstant.AUTH_TYPE_PERSONAL
import com.cash.app.manager.AccountInfoManager
import com.cash.app.net.NetRequestExecutor
import com.cash.app.net.RetrofitClient
import com.cash.app.ui.auth.api.AuthAPIService
import com.cash.app.ui.auth.model.AuthPersonalInfoResponse
import com.cash.app.ui.auth.model.AuthTypeRequest
import com.cash.app.ui.auth.model.StepTwoRequest

class AuthPersonalViewModel : BaseViewModel() {

    private val authAPIService by lazy {
        RetrofitClient.createService(AuthAPIService::class.java)
    }

    private val _authPersonalInfo = MutableLiveData<AuthPersonalInfoResponse>()
    val authPersonalInfo: LiveData<AuthPersonalInfoResponse> = _authPersonalInfo

    private val _submitSuccess = MutableLiveData<Boolean>()
    val submitSuccess: LiveData<Boolean> = _submitSuccess

    var currentOptionType: String = ""

    var educationId: String = ""
    var maritalId: String = ""
    var childrenNumId: String = ""
    var careerId: String = ""
    var incomeId: String = ""
    var firstContactId: String = ""
    var secondContactId: String = ""

    // First contact info
    var firstContactName: String = ""
    var firstContactPhone: String = ""

    // Second contact info
    var secondContactName: String = ""
    var secondContactPhone: String = ""

    fun getAuthInfoByPersonal() {
        launchRequest(request = {
            NetRequestExecutor.baseNetRequest {
                authAPIService.getAuthInfoByPersonal(AuthTypeRequest(authType = AUTH_TYPE_PERSONAL))
            }
        }, onSuccess = { response ->
            response?.let {
                _authPersonalInfo.value = it
            }
        })
    }

    fun submitStepTwo(context: Context, email: String) {
        if (educationId.isEmpty()) {
            showToast(context, R.string.auth_error_select_education)
            return
        }
        if (maritalId.isEmpty()) {
            showToast(context, R.string.auth_error_select_marital)
            return
        }
        if (childrenNumId.isEmpty()) {
            showToast(context, R.string.auth_error_select_children)
            return
        }
        if (careerId.isEmpty()) {
            showToast(context, R.string.auth_error_select_career)
            return
        }
        if (incomeId.isEmpty()) {
            showToast(context, R.string.auth_error_select_income)
            return
        }
        if (firstContactName.isEmpty() || firstContactPhone.isEmpty() || firstContactId.isEmpty()) {
            showToast(context, R.string.auth_error_first_contact)
            return
        }
        if (secondContactName.isEmpty() || secondContactPhone.isEmpty() || secondContactId.isEmpty()) {
            showToast(context, R.string.auth_error_second_contact)
            return
        }
        if (firstContactPhone == secondContactPhone) {
            showToast(context, R.string.auth_error_same_contacts)
            return
        }
        if (firstContactPhone == AccountInfoManager.getUserMobileNumber()) {
            showToast(context, R.string.auth_error_own_number)
            return
        }
        if (secondContactPhone == AccountInfoManager.getUserMobileNumber()) {
            showToast(context, R.string.auth_error_own_number)
            return
        }
        if (email.isEmpty()) {
            showToast(context, R.string.auth_error_invalid_email)
            return
        }
        var realEmail = email
        val emailSuffix = context.getString(R.string.auth_p_email_g)
        if (!email.contains(emailSuffix)) {
            realEmail += context.getString(R.string.auth_p_email_g)
        }

        val request = StepTwoRequest(
            educate = educationId.toInt(),
            marry = maritalId.toInt(),
            childNum = childrenNumId.toInt(),
            industry = careerId.toInt(),
            workMoney = incomeId.toInt(),
            directName = firstContactName,
            directPhone = firstContactPhone,
            directRelation = firstContactId.toInt(),
            otherName = secondContactName,
            otherPhone = secondContactPhone,
            otherRelation = secondContactId.toInt(),
            email = realEmail
        )

        launchRequest(request = {
            NetRequestExecutor.baseNetRequest {
                authAPIService.submitStepTwo(request)
            }
        }, onSuccess = { // rsp is empty
            _submitSuccess.value = true
        })
    }

    private fun showToast(context: Context, strResId: Int) {
        Toast.makeText(context, context.getString(strResId), Toast.LENGTH_SHORT).show()
    }
} 